package com.java.tutorials.jdk7.fileattribute;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.AclEntry;
import java.nio.file.attribute.AclEntryFlag;
import java.nio.file.attribute.AclEntryPermission;
import java.nio.file.attribute.AclFileAttributeView;
import java.util.List;
import java.util.Set;
/**
 * Created by xuzhike on 2016/1/24.
 * http://www.java2s.com/Code/Java/JDK-7/ACLAttribute.htm
 */
public class ACLAttributeTest {
    public static void main(String[] args) throws Exception {
        Path path = Paths.get("e:/yjbWechat.txt");
        AclFileAttributeView view = Files.getFileAttributeView(path,
                AclFileAttributeView.class);
        List<AclEntry> aclEntryList = view.getAcl();
        for (AclEntry entry : aclEntryList) {
            System.out.println("User Principal Name: " + entry.principal().getName());
            System.out.println("ACL Entry Type: " + entry.type());
            displayEntryFlags(entry.flags());
            displayPermissions(entry.permissions());
            System.out.println();
        }
    }

    private static void displayPermissions(Set<AclEntryPermission> permissionSet) {
        if (permissionSet.isEmpty()) {
            System.out.println("No Permissions present");
        } else {
            System.out.println("Permissions");
            for (AclEntryPermission permission : permissionSet) {
                System.out.println(permission.name() + " ");
            }
        }
    }

    private static void displayEntryFlags(Set<AclEntryFlag> flagSet) {
        if (flagSet.isEmpty()) {
            System.out.println("No ACL Entry Flags present");
        } else {
            System.out.println("ACL Entry Flags");
            for (AclEntryFlag flag : flagSet) {
                System.out.println(flag.name() + " ");
            }
        }
    }
}
